package com.tenton.service.impl;

import com.tenton.entity.User;
import com.tenton.mapper.UserMapper;
import com.tenton.service.UserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author td
 * @since 2022-04-07
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    @Resource
    private UserMapper userMapper;
    
    @Override
    public boolean insertUser(User user) {
        boolean flag = false;
        try {
            userMapper.insert(user);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    
    @Override
    public boolean updateUser(User user) {
        Assert.isTrue(user.getId() != 0,"Id不能为空");
        boolean flag = false;
        try {
            userMapper.updateById(user);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    
    @Override
    public boolean deleteUser(Integer id) {
        Assert.isTrue(id != 0,"Id不能为空");
        boolean flag = false;
        try {
            userMapper.deleteById(id);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }
    
    @Override
    public User getById(Integer id) {
        return userMapper.selectById(id);
    }
    
    @Override
    public List<User> getAll() {
        return userMapper.getAll();
    }
}
